package net.zjitc.controller;

import net.zjitc.common.ResponseResult;
import net.zjitc.entity.Attribute;
import net.zjitc.service.AttributeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
public class AttributeContrller {
    @Autowired
    private AttributeService attributeService;

    /**
     * 参数列表
     * @param id
     * @param sel
     * @return
     */
    @GetMapping("categories/{id}/attributes")
    public ResponseResult selectList(@PathVariable Integer id,@RequestParam String sel){
        ResponseResult result=new ResponseResult();
        List<Attribute> attributes= attributeService.select(id,sel);
        if (attributes.size()!=0){
            result.setSuccess("获取成功",attributes);
        }else {
            result.setError("没有查询到数据");
        }
        return result;
    }

    /**
     * 添加动态参数或者静态属性
     * @param id
     * @param attr_name
     * @param attr_sel
     * @return
     */
    @PostMapping("categories/{id}/attributes")
    public ResponseResult add(@PathVariable  Integer id,@RequestParam String attr_name,@RequestParam String attr_sel){
        ResponseResult result=new ResponseResult();
        Attribute attribute1=attributeService.addAttr(id, attr_name,attr_sel);
        result.setCreateSuccess("创建成功",attribute1);
        return result;
    }

    /**
     * 删除参数
     * @param id
     * @param attrid
     * @return
     */
    @DeleteMapping("categories/{id}/attributes/{attrid}")
    public ResponseResult delete(@PathVariable  Integer id,
                                 @PathVariable  Integer attrid){
        ResponseResult result=new ResponseResult();
        attributeService.delete(id,attrid);
        result.setSuccess("删除成功");
        return result;
    }

    /**
     * 根据 ID 查询参数
     * @param id
     * @param attrid
     * @param attr_sel
     * @param attr_vals
     * @return
     */
    @GetMapping("categories/{id}/attributes/{attrid}")
    public ResponseResult selectById(@PathVariable  Integer id,
                                 @PathVariable  Integer attrid,
                                     @RequestParam String attr_sel,
                                     @RequestParam(required = false) String attr_vals){
        ResponseResult result=new ResponseResult();
        Attribute attribute=attributeService.selectByAttrId(id,attrid,attr_sel,attr_vals);
        result.setSuccess("获取成功",attribute);
        return result;
    }

    /**
     * 编辑提交参数
     * @param id
     * @param attrid
     *
     * @return
     */
    @PutMapping("categories/{id}/attributes/{attrid}")
    public ResponseResult update(@PathVariable  Integer id,
                                 @PathVariable  Integer attrid,
                                 @RequestBody Attribute attribute){
        ResponseResult result=new ResponseResult();
        Attribute attribute1=attributeService.update(id, attrid,attribute);
        result.setSuccess("更新成功",attribute1);
        return result;
    }
}
